/*
 * Read the problem carefully! The first number is the place that 1 should be in
 * the sequence, not the first number of the sequence.
 * Could be done either as Longest Increasing Sequence or Longest Common Subsequence
 * This program implements the later one.
 */

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

int n, result, order[31], guess[31], d[31][31];

int main()
{
	string s;

	scanf("%d\n", &n);

	getline(cin, s);
	istringstream ss(s);
	for (int i = 1; i <= n; i++)
	{
		int x;
		ss >> x;
		order[x] = i;
	}

	while (getline(cin, s))
	{
		istringstream ss(s);
		for (int i = 1; i <= n; i++)
		{
			int x;
			ss >> x;
			guess[x] = i;
		}

		for (int i = 0; i <= n; i++)
		{
			d[0][i] = 0;
			d[i][0] = 0;
		}

		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				if (order[i] == guess[j]) d[i][j] = d[i-1][j-1] + 1;
				else d[i][j] = max(d[i-1][j], d[i][j-1]);

		cout << d[n][n] << endl;
	}
	return 0;
}
